package com.mawell.borum.demo;

import com.mawell.borum.OperandProperty;
import net.antonioshome.swing.treewrapper.DeepCopyNode;

import javax.swing.*;
import java.awt.*;

public class MaxHitsProperty extends OperandProperty {

    private int maxNumberOfHits = 1;


    public int getMaxNumberOfHits() {
        return maxNumberOfHits;
    }

    public void setMaxNumberOfHits(int maxNumberOfHits) {
        this.maxNumberOfHits = maxNumberOfHits;
    }

    public ExampleTranslation getTranslation() {
        if(getTranslationForProperty() != null && getTranslationForProperty() instanceof ExampleTranslation) {
            return (ExampleTranslation) getTranslationForProperty();
        } else {
            return new ExampleTranslation();
        }
    }

    public String getName() {
        return getTranslation().getLabelMaxHits();
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(16);
        sb.append(getTranslation().getLabelMax()).append(' ').append(getMaxNumberOfHits()).append(' ');
        if(getMaxNumberOfHits() == 1) {
            sb.append(getTranslation().getLabelHit());
        } else {
            sb.append(getTranslation().getLabelHits());
        }
        return sb.toString();
    }

   public boolean edit(Component comp) {
        String[] options = {getTranslation().getLabelSave(), getTranslation().getLabelCancel()};
        EditPanel panel = new EditPanel();
        int result = JOptionPane.showOptionDialog(comp, panel, getName(), JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, getTranslation().getLabelSave());
        switch(result) {
            case 0:
                //Save pushed, save values and return true
                maxNumberOfHits = panel.getNumberOfHits();
                setDelete(panel.isDeleted());
                return true;
            default:
                //Cancel pushed
                return false;
        }
    }

    private class EditPanel extends JPanel {

        private JSpinner numberOfHitsField;
        private JCheckBox deleteCheckBox;

        private EditPanel() {
            init();
            new Thread(new Runnable() {
                public void run() {
                    while(true) {
                        if(numberOfHitsField.hasFocus()) return;

                        numberOfHitsField.requestFocusInWindow();

                        try {
                            Thread.sleep(50);
                        }
                        catch(InterruptedException e) {
                            // nothing to do
                        }
                    }
                }
            }).start();


        }

        public Integer getNumberOfHits() {
            return (Integer) numberOfHitsField.getValue();
        }


        public void init() {
            add(createMainPanel());
        }


        private JPanel createMainPanel() {
            JPanel mainPanel = new JPanel(new GridBagLayout());

            numberOfHitsField = new JSpinner(new SpinnerNumberModel(maxNumberOfHits, 1, 9999, 1));

            GridBagConstraints c = new GridBagConstraints();
            c.anchor = GridBagConstraints.NORTHWEST;
            c.weightx = 0; // add horizontal space to this component
            c.weighty = 0; // add vertical space to this component
            c.gridwidth = GridBagConstraints.REMAINDER; // end of row
            c.gridheight = 1;
            c.fill = GridBagConstraints.VERTICAL; // fill vertically and horizontally if bigger than expected

            mainPanel.add(numberOfHitsField, c);


            deleteCheckBox = new JCheckBox();

            c = new GridBagConstraints();
            c.anchor = GridBagConstraints.NORTHWEST;
            c.weightx = 0; // add horizontal space to this component
            c.weighty = 0; // add vertical space to this component
            c.gridwidth = GridBagConstraints.REMAINDER; // end of row
            c.gridheight = 1;
            c.fill = GridBagConstraints.VERTICAL; // fill vertically and horizontally if bigger than expected

            mainPanel.add(deleteCheckBox, c);

            return mainPanel;
        }

        public boolean isDeleted() {           
            return deleteCheckBox.isSelected();
        }
    }

    public DeepCopyNode deepCopy() {
        MaxHitsProperty copy = new MaxHitsProperty();
        copy.setMaxNumberOfHits(getMaxNumberOfHits());
        copy.setTranslationForProperty(getTranslationForProperty());

        return copy;
    }
}
